<?php

namespace App\Services\User\Department;

use Illuminate\Support\Facades\DB;
use App\Services\User\App\Applogic;
use App\Services\User\User\Userlogic;
use \App\Models\User\Department;
use \App\Models\User\DepartmentRole;
/**
 * 部门管理的逻辑层
 */
class DepartmentLogic
{
//    protected $name = 'role';
//    protected $config = 'db_config_user';
//
//    private $roleModel = '\app\serveruser\model\Role';
//    private $applyRoleModel = '\app\serveruser\model\ApplyRole';
//    private $userModel = '\app\serveruser\model\User';
//
//    private $appLogic = '\App\Services\User\App\Applogic';
//    private $userLogic = '\app\serveruser\logic\User';

    // 按分页获取部门列表
    public function list(array $data)
    {
        //isset($data['status']) ? $_where['status'] = $data['status'] : null;
        if($data['status'] !== ''){
            $_where[] = ['status','=',$data['status']];
        }
        if(isset($_where)){
            $list =Department::where($_where)
                ->orderBy('id', 'desc')
                ->paginate(10);
        }else{
            $list = Department::orderBy('id', 'desc')
                ->paginate(10);
        }

        return auto_success($list, '成功获取部门列表', '获取部门列表失败');
    }
    // 增加一条部门记录
    public function add(array $data): array
    {
        $data['status'] = '1';
        $data['menu'] = '{}';
        $data['create_time'] = NOW;
        $data['update_time'] = NOW;
        try{
            $res = Department::insert($data);
        }catch (\Exception $e) {
            // 当唯一键重复等报错时
            return server_err(null, '数据库异常:' . $e->getMessage());
        }
        return auto_return($res, '成功增加一个部门', '增加一个部门失败');
    }
    // 获取某个部门的详情
    public function getDepartmentInfo($data): array
    {
        $_where['id'] = $data['id'];
        $info = Department::where($_where)->first()->toArray();
        return auto_return($info, '成功获取该部门的详情');
    }
        // 编辑用户部门名,权重,和附加菜单信息
    public function edit(array $data): array
    {
        $_where[] = ['id','=',$data['id']];
        $data['update_time'] = NOW;
        try{
            $res = Department::where($_where)
                ->update($data);
        }catch (\Exception $e) {
            // 当唯一键重复等报错时
            return server_err(null, '数据库异常:' . $e->getMessage());
        }
        return auto_return($res, '成功编辑部门信息', '编辑部门信息失败');
    }
    // 启用一个部门
    public function start(array $data): array
    {
        isset($data['id']) ? $_where[] = ['id','=',$data['id']] : null;
        isset($data['name']) ? $_where[] = ['name','=',$data['name']] : null;
        if(!isset($_where)){
            return verify_err(null, '至少要传一个id或name参数');
        }
        $res = Department::where($_where)->update(['status'=>'1']);
        return auto_return($res, '成功启用一个部门', '启用一个部门失败');
    }
    // 禁用一个部门
    public function stop(array $data): array
    {
        isset($data['id']) ? $_where[] = ['id','=',$data['id']] : null;
        isset($data['name']) ? $_where[] = ['name','=',$data['name']] : null;

        $res = Department::where($_where)->update(['status'=>'0']);
        return auto_return($res, '成功禁用一个部门', '禁用一个部门失败');
    }

}
